package com.ckzp.weixin;


import com.ckzp.core.ConfigUtil;
import com.ckzp.core.Results;
import com.ckzp.core.util.CookieUtil;
import com.ckzp.core.util.JwtUtil;
import com.ckzp.core.util.StringUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by 51594 on 2022/8/31.
 */
public class PageSite {

    /**
     * @return
     */
    public static String getWebHost() {
        return ConfigUtil.getProperty("WEB_HOST");
    }

    /**
     * @return
     */
    public static String getWebHostChuan() {
        return ConfigUtil.getProperty("WEB_HOST_CHUAN");
    }

    /**
     * @return
     */
    public static String getWebHostWx() {
        return ConfigUtil.getProperty("WEB_HOST_WX");
    }


    /**
     * @return
     */
    public static String getUserIdName() {
        return ConfigUtil.getProperty("USER_ID", "USERID_FDD_NEW");
    }

    /**
     * @param request
     * @return
     */
    public static int getU_ID(HttpServletRequest request) {
        int u_id = getUID_fromSession(request);
        if (u_id == 0) {
            u_id = getUID_fromCookie(request);
        }
        if (u_id == 0) {
            u_id = getUID_fromJwt(request);
        }
        return u_id;
    }

    /**
     * @param request
     * @return
     */
    public static void setU_ID(HttpServletRequest request, HttpServletResponse response, int u_id) {
        if (u_id == 0) {
            CookieUtil.removeCookie(request, response, getUserIdName());
            request.getSession().removeAttribute(getUserIdName());
        } else {
            request.getSession().setAttribute(getUserIdName(), String.valueOf(u_id));
            JwtUtil.JwtSubject subject = new JwtUtil.JwtSubject();
            subject.setUserid(u_id);
            String jwt = JwtUtil.createJWT("", subject, JwtUtil.EXP_DATE);
            CookieUtil.setCookie(response, getUserIdName(), jwt);
        }
    }


    /**
     * 取得uid
     *
     * @param request
     * @return
     */
    public static int getUID_fromCookie(HttpServletRequest request) {
        String jwt = CookieUtil.getCookie(request, getUserIdName());
        Results results = JwtUtil.validateJWT(jwt);
        if (results.getCode() == 0) {
            return results.getObject(JwtUtil.JwtSubject.class).getUserid();
        }
        return 0;
    }

    /**
     * ��cookie��ȡuid
     *
     * @param request
     * @return
     */
    public static int getUID_fromSession(HttpServletRequest request) {
        String uid = String.valueOf(request.getSession().getAttribute(getUserIdName()));
        return StringUtil.getInt(uid, 0);
    }

    /**
     * @param request
     * @return
     */
    public static int getUID_fromJwt(HttpServletRequest request) {
        String jwt = request.getParameter("jwt");
        if (jwt == null) return 0;
        if ("test".equals(jwt)) return 123420;
        Results results = JwtUtil.validateJWT(jwt);
        if (results.getCode() == 0) {
            return results.getObject(JwtUtil.JwtSubject.class).getUserid();
        }
        return 0;
    }
}
